//package com.stack.common.utils;
//
//import com.emodor.attendance.entities.group.GroupSalaryReportDTO;
//import com.emodor.attendance.entities.group.WorkerGroupBaseDTO;
//import com.emodor.attendance.entities.group.criteria.WorkerGroupQueryCriteria;
//import com.emodor.attendance.entities.project.ProjectBaseDTO;
//import com.emodor.attendance.entities.user.SysUserDTO;
//import com.emodor.attendance.entities.worker.WorkerBankDTO;
//import com.emodor.attendance.entities.worker.WorkerReportDTO;
//import com.emodor.attendance.export.apachePOI.XWPFHelper;
//import com.emodor.attendance.export.spire.SpireExportWorkerPaySchedule;
//import com.emodor.attendance.facade.model.request.export.DownloadWorkerPayScheduleReportRequestVO;
//import com.emodor.attendance.facade.model.request.export.ProjectVO;
//import com.emodor.attendance.service.company.CompanyService;
//import com.emodor.attendance.service.project.CompanyProjectService;
//import com.emodor.attendance.service.worker.WorkerGroupService;
//import com.emodor.attendance.utils.MathGenerate;
//import com.spire.doc.Document;
//import com.spire.doc.FileFormat;
//import org.apache.dubbo.config.annotation.DubboReference;
//import org.apache.poi.xwpf.usermodel.XWPFDocument;
//import org.apache.poi.xwpf.usermodel.XWPFRun;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//@Component
//public class POIWordUtil {
//
//    private final Logger logger = LoggerFactory.getLogger(getClass());
//
//    @DubboReference
//    private CompanyProjectService projectService;
//    @DubboReference
//    private WorkerGroupService groupService;
//	@DubboReference
//	private CompanyService companyService;
//	/**
//	 * 1、统计及汇总数据
//	 * 2、拷贝一份word文档，及模版
//	 * 3、word文档模版填充数据
//	 * 4、word文档添加水印
//	 * 5、写到服务器中，生成文件
//	 * @param vo
//	 * @param templateFilePath
//	 * @param waterMarkPicture
//	 * @param randomDirPath
//	 * @param fileName
//	 * @throws IOException
//	 */
//	public void generateWorkerPayScheduleReportSpireDocument(DownloadWorkerPayScheduleReportRequestVO vo, SysUserDTO exporter, String templateFilePath, String waterMarkPicture, String randomDirPath, String fileName) throws IOException {
//		// 1、统计及汇总数据
//		SpireExportWorkerPaySchedule exportHelper = new SpireExportWorkerPaySchedule();
//		String projectId = groupService.getProjectIdByGroupId(vo.getGroupId());
//        ProjectBaseDTO project = projectService.getProjectBaseByProjectId(projectId);
//		String companyWatermarkLogo = companyService.fetchWatermarkCompanyLogoByCompanyId(exporter.getCurrentCompanyDTO().getId());
//		WorkerGroupQueryCriteria groupQueryCriteria = new WorkerGroupQueryCriteria();
//        groupQueryCriteria.setGroupId(vo.getGroupId());
//        List<WorkerGroupBaseDTO> groupList = groupService.listWorkerGroupCommonService(groupQueryCriteria);
//        ProjectVO projectVo = new ProjectVO();
//        projectVo.setProjectId(projectId);
//        projectVo.getGroupList().add(vo.getGroupId());
//
//		Map<String, Map<String, Double>> groupsIncomeMap = new HashMap<>();
//		Map<String, List<WorkerBankDTO>> groupsWorkerList = new HashMap<>();
//		Map<String, Map<String, WorkerReportDTO>> groupsWorkerReport = new HashMap<>();
//		computeGroupsIncomeMap(projectVo, vo.getStartDate(), vo.getEndDate(), groupsIncomeMap, groupsWorkerList, groupsWorkerReport);
//		logger.info("模板文件路径:"+templateFilePath);
//        // 2、拷贝一份word文档，及模版
//     	Document document = exportHelper.loadTemplateDocument(templateFilePath);
//     	int workerCounts = getWorkerCount(groupsWorkerList, groupsWorkerReport);
//     	exportHelper.createDocumentStyle(workerCounts, document);
// 		// 3、word文档模版填充数据
// 		document = exportHelper.fillDocumentData(companyWatermarkLogo,exporter, project, groupList, vo.getStartDate(), vo.getEndDate(), groupsIncomeMap, groupsWorkerList, groupsWorkerReport, document);
// 		// 4、word文档添加水印
// 		exportHelper.addDocumentAttachment(project.getName(), waterMarkPicture, document);
// 		String fakeFileName = MathGenerate.generateUUIdNonHorizontal() + ".docx";
// 		String fakeFilePathName = randomDirPath + File.separator + fakeFileName;
//		// 5、写到服务器中
//		document.saveToFile(fakeFilePathName, FileFormat.Docx_2013);
//		String realFilePathName = randomDirPath + File.separator + fileName;
//		XWPFHelper fileHelper = new XWPFHelper();
//		XWPFDocument realDocment = fileHelper.openDoc(fakeFilePathName);
//		XWPFRun removeRun = realDocment.getParagraphs().get(0).getRuns().get(0);
//		removeRun.setText("", 0);
//		removeRun.setText("");
//		fileHelper.saveDocument(realDocment, realFilePathName);
//	}
//
//	private int getWorkerCount(Map<String, List<WorkerBankDTO>> groupsWorkerList,
//			Map<String, Map<String, WorkerReportDTO>> groupsWorkerReport) {
//		int workerCounts = 0;
//		for (String groupId : groupsWorkerList.keySet()) {
//			List<WorkerBankDTO> workerBank = groupsWorkerList.get(groupId);
//			Map<String, WorkerReportDTO> groupWorkerMap = groupsWorkerReport.get(groupId);
//			for (WorkerBankDTO worker : workerBank) {
//				if (!groupWorkerMap.containsKey(worker.getWorkerId())) {
//					continue;
//				}
//				workerCounts++;
//			}
//		}
//		return workerCounts;
//	}
//
//	private void computeGroupsIncomeMap(ProjectVO projectVO, String startDate, String endDate , Map<String, Map<String, Double>> groupsIncomeMap,
//										Map<String, List<WorkerBankDTO>> groupsWorkerList,
//										Map<String, Map<String, WorkerReportDTO>> groupWorkerReport) {
//	     for (String groupId : projectVO.getGroupList()) {
//	    	 Map<String, Double> incomeMap = new HashMap<>();
//		     Map<String, WorkerReportDTO> workerReportMap = new HashMap<>();
//	    	 GroupSalaryReportDTO reportDTO = groupService.getGroupSalaryReport(groupId, startDate, endDate);
//	    	 List<WorkerReportDTO> workerList = reportDTO.getWorkerReportList();
//	    	 if (workerList != null && !workerList.isEmpty()) {
//	    		 for (WorkerReportDTO worker : workerList) {
//		    		 if (incomeMap.get(worker.getId()) == null) {
//		    			 incomeMap.put(worker.getId(), 0.0);
//		             }
//		             workerReportMap.put(worker.getWorkerId(), worker);
//		             double totalMondey = incomeMap.get(worker.getId());
//		             totalMondey += worker.getIncome() + worker.getOtIncome();
//		             incomeMap.put(worker.getId(), totalMondey);
//	    		 }
//	    	 }
//
//	    	 List<WorkerBankDTO> workerBankInfoList = groupService.listWorkerBankInfoByGroupId(groupId);
//	    	 groupsIncomeMap.put(groupId, incomeMap);
//	    	 groupsWorkerList.put(groupId, workerBankInfoList);
//	    	 groupWorkerReport.put(groupId, workerReportMap);
//	     }
//	}
//
//}